package main;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketSend {

	private String ip;
	private int port;

	public SocketSend(String ip, int port) {

		this.ip = ip;
		this.port = port;

	}

	public void SendMessage(String message) {

		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		Socket clientSocket = null;

		try {
			clientSocket = new Socket(ip, port);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.writeObject(message);

			ois = new ObjectInputStream(clientSocket.getInputStream());
			String receive = (String) ois.readObject();

			System.out.println(receive);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				oos.close();
				ois.close();
				clientSocket.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
